package cn.spring.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.spring.bean.Orders;
import cn.spring.bean.Today;
import cn.spring.dao.OrdersMapper;
import cn.spring.dao.ShoppingcartMapper;
import cn.spring.service.OrdersService;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
	@Resource
	private OrdersMapper dao;
	
	@Resource
	private ShoppingcartMapper sDao;

	@Override
	public int save(Orders ord) {
		// TODO Auto-generated method stub
		ord.setOrdertime(new Date());
		return dao.insert(ord);
	}

	@Override
	public List<Orders> selectByState(Integer empId) {
		// TODO Auto-generated method stub
		return dao.selectByState(empId);
	}

	@Override
	public int updateTimeAndState(Integer empId,String time, Integer id) {
		// TODO Auto-generated method stub
		sDao.updateIsGouByOrdId(empId, id);
		return dao.updateTimeAndState(time, id);
	}

	@Override
	public List<Orders> selectToDay(Integer empId) {
		// TODO Auto-generated method stub
		return dao.selectToDay(empId);
	}

	@Override
	public List<Orders> selectAll(Integer empId) {
		// TODO Auto-generated method stub
		return dao.selectAll(empId);
	}

	@Override
	public int fa(Integer id) {
		// TODO Auto-generated method stub
		return dao.updateState(4, id);
	}

	@Override
	public List<Map<String, Object>> selectLi(String startTime, String endTime) {
		// TODO Auto-generated method stub
		return dao.selectLi(startTime, endTime);
	}

	@Override
	public Map<Today, List<Map<String, Object>>> today(Integer empId,
			String startTime, String endTime) {
		// TODO Auto-generated method stub
		List<Today> obj=dao.toDay(empId, startTime, endTime);
		Map<Today, List<Map<String, Object>>> list=new LinkedHashMap<Today, List<Map<String, Object>>>();
		for(Today ob:obj){
			List<Map<String, Object>> cList=sDao.selectByState1(ob.getOrdid());
			list.put(ob, cList);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> selectMon(String time) {
		// TODO Auto-generated method stub
		return dao.selectMon(time+"-01",time+"-32");
	}

	@Override
	public List<Map<String, Object>> selectLiCom(String startTime, String endTime) {
		// TODO Auto-generated method stub
		return dao.selectLiCom(startTime,endTime);
	}

	@Override
	public List<Map<String, Object>> selectLiComWan(String startTime, String endTime,Integer empId) {
		// TODO Auto-generated method stub
		return dao.selectLiComWan(startTime, endTime,empId);
	}

	@Override
	public List<Map<String, Object>> selectLiComByWord(String startTime, String endTime,String word) {
		// TODO Auto-generated method stub
		word="%"+word+"%";
		return dao.selectLiComByWord(startTime, endTime,word);
	}

	@Override
	public List<Map<String, Object>> selectLiByWord(String startTime, String endTime, String word) {
		// TODO Auto-generated method stub
		word="%"+word+"%";
		return dao.selectLiByWord(startTime, endTime, word);
	}

	@Override
	public int deleteOrd(int id) {
		// TODO Auto-generated method stub
		try {
			int count=sDao.deleteByOrdId(id);
			if(count>0) {
				count=dao.deleteByPrimaryKey(id);
				return count;
			}else {
				return 0;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Map<Today, List<Map<String, Object>>> selectByUserIdMon(String time, int eId) {
		// TODO Auto-generated method stub
		List<Today> obj=dao.selectByUserIdMon(eId,time+"-01",time+"-32");
		Map<Today, List<Map<String, Object>>> list=new LinkedHashMap<Today, List<Map<String, Object>>>();
		for(Today ob:obj){
			List<Map<String, Object>> cList=sDao.selectByState1(ob.getOrdid());
			list.put(ob, cList);
		}
		return list;
	}
	
}
